package com.internetCafes.spms.web.customer.common.mode;

import com.internetCafes.spms.common.utils.ApplicationContextUtils;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.customer.common.component.customer.IEntCustomerComponent;
import com.internetCafes.spms.web.customer.model.ExcelImportResultRes;
import com.internetCafes.spms.web.customer.model.customer.CustomerBatchSaveReq;
import com.internetCafes.spms.web.customer.model.customer.CustomerSaveReq;
import com.internetCafes.spms.web.customer.model.customer.CustomerUpdateReq;

public class CustomerMode {

    /**
     * 客户批量保存
     *
     * @param req 请求参数
     * @return 保存结果
     */
    public static ExcelImportResultRes batchSave(CustomerBatchSaveReq req) {
        for (CustomerServiceEnum customerService : CustomerServiceEnum.values()) {
            if (customerService.getTypeId().equals(req.getTypeId())
                    && customerService.getEnterpriseFlag().equals(req.getEnterpriseFlag())) {
                IEntCustomerComponent component = ApplicationContextUtils.getBean(customerService.getServiceBeanName());
                return component.batchSave(req);
            }
        }
        throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
    }

    /**
     * 策略模式-客户信息编辑
     *
     * @param req 请求参数
     * @return true/false
     */
    public static boolean update(Integer typeId, Integer enterpriseFlag, CustomerUpdateReq req) {
        for (CustomerServiceEnum customerService : CustomerServiceEnum.values()) {
            if (customerService.getTypeId().equals(typeId)
                    && customerService.getEnterpriseFlag().equals(enterpriseFlag)) {
                IEntCustomerComponent component = ApplicationContextUtils.getBean(customerService.getServiceBeanName());
                return component.update(req);
            }
        }
        throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
    }

    /**
     * 策略模式-客户信息新增
     *
     * @param req 请求参数
     * @return true/false
     */
    public static boolean save(CustomerSaveReq req) {
        for (CustomerServiceEnum customerService : CustomerServiceEnum.values()) {
            if (customerService.getTypeId().equals(req.getTypeId())
                    && customerService.getEnterpriseFlag().equals(req.getEnterpriseFlag())) {
                IEntCustomerComponent component = ApplicationContextUtils.getBean(customerService.getServiceBeanName());
                return component.save(req);
            }
        }
        throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
    }

    /**
     * 区域客户 typeId = 3 enterpriseFlag = 1
     * 意向客户-个人 typeId = 2 enterpriseFlag = 0
     * 意向客户-企业 typeId = 2 enterpriseFlag = 1
     * 成交客户-个人 typeId = 1 enterpriseFlag = 0
     * 成交客户-企业 typeId = 1 enterpriseFlag = 1
     */
    public enum CustomerServiceEnum {
        AREA_ENT(3, 1, "areaCustomerComponent"),
        INTENTION_PERSON(2, 0, "intentionPersonCustomerComponent"),
        INTENTION_ENT(2, 1, "intentionEntCustomerComponent"),
        DEAL_PERSON(1, 0, "dealPersonCustomerComponent"),
        DEAL_ENT(1, 1, "dealEntCustomerComponent"),
        ;
        /**
         * 客户类别 1-成交客户 2-意向客户 3-一般客户
         */
        private Integer typeId;

        /**
         * 是否是企业客户 0-默认 1-企业客户
         */
        private Integer enterpriseFlag;

        /**
         * service 对应的bean
         */
        private String serviceBeanName;

        CustomerServiceEnum(Integer typeId, Integer enterpriseFlag, String serviceBeanName) {
            this.typeId = typeId;
            this.enterpriseFlag = enterpriseFlag;
            this.serviceBeanName = serviceBeanName;
        }

        public Integer getTypeId() {
            return typeId;
        }

        public Integer getEnterpriseFlag() {
            return enterpriseFlag;
        }

        public String getServiceBeanName() {
            return serviceBeanName;
        }
    }
}
